package cn.lanqiao.ui;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

import cn.lanqiao.dao.UserDao;
import cn.lanqiao.dao.impl.UserDaoImpl;
import cn.lanqiao.entity.Student;
import cn.lanqiao.service.UserService;
import cn.lanqiao.service.impl.UserServiceImpl;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.border.LineBorder;
import java.awt.Color;

public class StudentInformation extends JInternalFrame {
	JDesktopPane desktopPane = null;
	private static final long serialVersionUID = -2207339285801304848L;
	private JTable table;
	private JTextField txtSearch;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					StudentInformation frame = new StudentInformation();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public StudentInformation() {
		setTitle("学籍信息管理");
		setClosable(true);
		setBounds(100, 100, 892, 418);
		getContentPane().setLayout(null);
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(0, 0, 876, 194);
		getContentPane().add(scrollPane);
		table = new JTable();
		table.setFillsViewportHeight(true);
		table.setBorder(new LineBorder(new Color(0, 0, 0)));
		table.setFont(new Font("微软雅黑", Font.PLAIN, 20));
		table.setToolTipText("");

		//设置表格数据居中
		DefaultTableCellRenderer cr = new DefaultTableCellRenderer();
		cr.setHorizontalAlignment(JLabel.CENTER);
		table.setDefaultRenderer(Object.class, cr);
		//设置行高
		table.setRowHeight(30);

		bindJTable();
		scrollPane.setViewportView(table);

		JButton updateStu = new JButton("修改信息");
		updateStu.setFont(new Font("微软雅黑", Font.PLAIN, 20));
		updateStu.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// 修改信息
				// 判斷是否选择需要修改的行
				int count = table.getSelectedRowCount();
				if (count == 0) {
					JOptionPane.showMessageDialog(null, "请选择需要修改的数据！");
					return;
				}
				String sid, sname, sgender, scollege, smajor, sclass;
				int rowNum = table.getSelectedRow();// 获取行号
				// 获取单元格的值
				// 获取行中数据
				sid = table.getValueAt(rowNum, 0).toString();
				sname = table.getValueAt(rowNum, 1).toString();
				sgender = table.getValueAt(rowNum, 2).toString();
				scollege = table.getValueAt(rowNum, 3).toString();
				smajor = table.getValueAt(rowNum, 4).toString();
				sclass = table.getValueAt(rowNum, 5).toString();

				Student student = new Student(sid, sname, sgender, scollege, smajor, sclass);
				UpdateStudent updatestudent = new UpdateStudent(student, table);
				updatestudent.setVisible(true);
			}
		});
		updateStu.setBounds(27, 291, 125, 37);
		getContentPane().add(updateStu);

		JButton addStu = new JButton("添加学生");
		addStu.setFont(new Font("微软雅黑", Font.PLAIN, 20));
		addStu.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// 学籍录入
				AddStudent addstudent = new AddStudent(StudentInformation.this);
				addstudent.setVisible(true);
			}
		});
		addStu.setBounds(463, 291, 119, 37);
		getContentPane().add(addStu);

		JButton exportStu = new JButton("学籍信息导出");
		exportStu.setFont(new Font("微软雅黑", Font.PLAIN, 20));
		exportStu.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// 导出
				String[] headers = { "学号", "姓名", "性别", "学院", "专业", "班级", "入学时间" };
				UserService student = new UserServiceImpl();// 创建一个学生对象
				DefaultTableModel model = new DefaultTableModel(student.lists(), headers);
				final JTable table = new JTable(model);
				UserDao export = new UserDaoImpl();
				try {
					export.exportTable(table, new File("学籍信息.txt"));
					File file = new File("..\\Edu_admin_system\\学籍信息.txt");
					if (file.exists()) {
						JOptionPane.showMessageDialog(null, "导出成功不成功自己没点那啥数？");
						System.out.println("文件存在");
					} else {
						System.out.println("不存在");
					}
				} catch (IOException e1) {
					e1.printStackTrace();
					System.out.println(e1.getMessage());
				}
			}
		});

		exportStu.setBounds(667, 291, 166, 37);
		getContentPane().add(exportStu);

		JButton deleteStu = new JButton("删除学生");
		deleteStu.setFont(new Font("微软雅黑", Font.PLAIN, 20));
		deleteStu.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// 删除学生
				// 删前确认
				int count = table.getSelectedRowCount();
				if (count != 0) {
					int val = JOptionPane.showConfirmDialog(null, "确认删除吗？", "删除确认", JOptionPane.YES_NO_OPTION);
					if (val == 1) {// 取消
						return;
					}
				} else {
					JOptionPane.showMessageDialog(null, "请选择需要删除的数据！");
					return;
				}

				int rowNum = table.getSelectedRow();// 获取行号
				// 获取单元格的值
				String id = table.getValueAt(rowNum, 0).toString();// 获取行中数据
				UserService remove = new UserServiceImpl();
				remove.deleteStu(id);
				bindJTable();
				scrollPane.setViewportView(table);
			}
		});
		deleteStu.setBounds(246, 291, 119, 37);
		getContentPane().add(deleteStu);

		txtSearch = new JTextField();
		txtSearch.setToolTipText("");
		txtSearch.setHorizontalAlignment(SwingConstants.LEFT);
		txtSearch.setBounds(622, 227, 113, 30);
		getContentPane().add(txtSearch);
		txtSearch.setColumns(10);

		JLabel label = new JLabel("学号：");
		label.setFont(new Font("微软雅黑", Font.PLAIN, 20));
		label.setBounds(532, 226, 76, 27);
		getContentPane().add(label);

		JButton btnSearch = new JButton("查找");
		btnSearch.setFont(new Font("微软雅黑", Font.PLAIN, 20));
		btnSearch.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// 查找
				// 取文本框数据
				String sid = txtSearch.getText();
				if (sid.isEmpty()) {
					JOptionPane.showMessageDialog(null, "请输入学号！");
				} else {
					// 调业务层
					cn.lanqiao.service.UserService score = new cn.lanqiao.service.impl.UserServiceImpl();
					Student information = score.getStudentinformation(sid);
					String[] header = new String[] { "学号", "姓名", "性别", "学院", "专业", "班级", "入学时间" };
					table.setModel(new DefaultTableModel(new Object[][] {
							{ sid, information.getSname(), information.getSgender(), information.getScollege(),
									information.getSmajor(), information.getSclass(), information.getSenrol() }, },
							header));
					table.getColumnModel().getColumn(2).setPreferredWidth(47);
					table.getColumnModel().getColumn(3).setPreferredWidth(147);
					table.getColumnModel().getColumn(5).setPreferredWidth(48);
				}
			}
		});
		btnSearch.setBounds(749, 224, 84, 31);
		getContentPane().add(btnSearch);
	}

	public void bindJTable() {
		UserService service = new UserServiceImpl();
		table.setModel(new DefaultTableModel(service.lists(), new String[] { "\u5B66\u53F7", "\u59D3\u540D",
				"\u6027\u522B", "\u5B66\u9662", "\u4E13\u4E1A", "\u73ED\u7EA7", "\u5165\u5B66\u65F6\u95F4" }));
		table.getColumnModel().getColumn(2).setPreferredWidth(47);
		table.getColumnModel().getColumn(3).setPreferredWidth(147);
		table.getColumnModel().getColumn(5).setPreferredWidth(48);
	}
}
